package com.itheima.motorcycletype.controller;

import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSON;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.itheima.motorcycletype.dao.LogoMapper;
import com.itheima.motorcycletype.dao.MotorcycleTypeMapper;
import com.itheima.motorcycletype.pojo.Logo;
import com.itheima.motorcycletype.pojo.MotorcycleType;
import com.itheima.motorcycletype.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private MotorcycleTypeMapper motorcycleTypeMapper;

    @Autowired
    private LogoMapper logoMapper;

    @PostMapping("/logoReport")
    public Result logoReport(Map map) {
        String filePath = "D:\\车型库.xlsx";
//第一个1代表sheet1, 第二个1代表从第几行开始读取数据，行号最小值为0,sheet1为车标
        Sheet sheet1 = new Sheet(1, 4);
        List<Object> objects1 = ExcelUtil.readLessThan1000RowBySheet(filePath, sheet1);
        System.out.println("objects1 = " + objects1);
        for (Object logo : objects1) {
            System.out.println("logo = " + logo);
            List<String> l = (List<String>) logo;
            System.out.println(l);
            Logo ll = new Logo();
            ll.setId(Integer.parseInt(l.get(0)));
            ll.setName(l.get(1));
            ll.setCountry(l.get(2));
            //map.put("logo",logo);
            //System.out.println("map = " + map);
            //logoMapper.insert((Logo) map);
            logoMapper.insertSelective(ll);
        }

        return new Result(true, StatusCode.OK, "导入成功");
    }

    @PostMapping("/motorcycleTypeMapperReport")
    public Result motorcycleTypeMapperReport(){
        String filePath = "D:\\车型库.xlsx";
        //sheet2为车型
        Sheet sheet2 = new Sheet(2, 5);
        List<Object> objects2 = ExcelUtil.readLessThan1000RowBySheet(filePath, sheet2);
        for (Object motorcycleType : objects2) {
            List<String> m1= (List<String>) motorcycleType;
            MotorcycleType m2 = new MotorcycleType();
            m2.setId(Integer.parseInt(m1.get(0)));
            m2.setName(m1.get(1));
            m2.setCode(Integer.parseInt(m1.get(2)));
            m2.setLogoId(Integer.parseInt(m1.get(2)));
            motorcycleTypeMapper.insertSelective(m2);
        }
        return new Result(true, StatusCode.OK, "导入成功");
    }
}
